/*
 * jconfig.h
 *
 *  Created on: Dec 2, 2010
 *      Author: fify
 */

#ifndef JCONFIG_H_
#define JCONFIG_H_

#include "jsocket.h"
#include <string>

using std::string;

namespace CncrJConfig
{

enum JStatus {
	STATUS_NONE,		// Status when the server is not connected, or when the update_status()
						// function cannot decide which status we are in.
	STATUS_LOGIN,		// When the JunOS needs the user to offer username and password.
	STATUS_ROOT,		// Logged in as root user. This is different from normal users, for which the
						// JunOS with go directly into cli.
	STATUS_CLI,			// In JunOS cli console.
	STATUS_CONFIG		// In configure mode.
};

class JConfig
{
	JSocket m_sock;

	string m_err_msg;

	JStatus m_status;
	string m_edit_path;	// Only available when m_status == STATUS_CONFIG.

public:
	JConfig();
	virtual ~JConfig();

	int init(string host, int port);

	int enter_status(string user, string passwd, JStatus status);

	int load_cmds(vector<string> &cmds);
	int load_config_file(string filename);

	void perror(const char *msg);

	// Executing a single configuration command.
	int configure(string command);
	int check_and_commit();

	// If the JunOS has started and ready to accept configurations.
	int junos_started();

	JStatus get_status()
	{
		return m_status;
	}
	JSocket* get_sock()
	{
		return &m_sock;
	}

private:
	int try_login(string user, string passwd);
	int enter_cli(string user, string passwd);
	int enter_config();

	int exit(int force = 0);

	int update_status();
};

} // End of namespace;
#endif /* JCONFIG_H_ */
